package cherry.data.service.provider;


import cherry.data.mapper.CrHomePageMapper;
import cherry.data.service.CherryHomePageService;
import cherry.data.util.Result;
import cherry.data.view.request.CherryHomePageRequest;
import cherry.data.view.response.CherryHomePageResponse;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 谢宇翔
 * @version 1.0.0
 * @ClassName CherryHomePageServiceImpl.java
 * @Description 数据访问主页，数据展示提供，接口实现
 * @createTime 2022年02月06日 15:15:00
 */
@Log4j2
@RestController
public class CherryHomePageServiceImpl implements CherryHomePageService {


    @Resource
    private CrHomePageMapper crHomePageMapper;

    /**
     * @param request
     * @ClassName CherryHomePageService.java
     * @author 谢宇翔
     * @version 1.0.0
     * @Description 分页查询数据
     * @createTime 2022年02月06日 15:35:00
     */
    @Override
    public Result<PageInfo<CherryHomePageResponse>> findPage(long userId,@RequestBody CherryHomePageRequest request) {
        CherryHomePageResponse response = crHomePageMapper.findPage(request);
        List<CherryHomePageResponse> cherryHomePageResponses=new ArrayList<>();
        cherryHomePageResponses.add(response);
        Integer pageNum = request.getPageNum();
        Integer pageSize = request.getPageSize();
        int num = (pageNum - 1) * pageSize;
        int size = pageNum * pageSize;
        //分页
        List<CherryHomePageResponse> responseList = cherryHomePageResponses.stream().limit(size).skip(num).collect(Collectors.toList());
        PageInfo<CherryHomePageResponse> pageInfo = new PageInfo<>();
        //数据总条数
        pageInfo.setTotal(responseList.size());
        //当页数据
        pageInfo.setList(responseList);
        //页码
        pageInfo.setPageNum(pageNum);
        //每页长度
        pageInfo.setPageSize(pageSize);
        return Result.success(pageInfo);
    }
}
